package com.ruoyi.demo.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.mybatis.core.page.PageQuery;
import com.ruoyi.common.mybatis.core.page.TableDataInfo;
import com.ruoyi.demo.domain.TestCase;
import com.ruoyi.demo.domain.bo.TestCaseBo;
import com.ruoyi.demo.domain.vo.TestCaseVo;
import com.ruoyi.demo.mapper.TestCaseMapper;
import com.ruoyi.demo.service.ITestCaseService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 案件管理Service业务层处理
 *
 * @author ruoyi
 * @date 2023-04-21
 */
@RequiredArgsConstructor
@Service
public class TestCaseServiceImpl implements ITestCaseService {

    private final TestCaseMapper baseMapper;

    /**
     * 查询案件管理
     */
    @Override
    public TestCaseVo queryById(Long id) {
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询案件管理列表
     */
    @Override
    public TableDataInfo<TestCaseVo> queryPageList(TestCaseBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<TestCase> lqw = buildQueryWrapper(bo);
        Page<TestCaseVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询案件管理列表
     */
    @Override
    public List<TestCaseVo> queryList(TestCaseBo bo) {
        LambdaQueryWrapper<TestCase> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<TestCase> buildQueryWrapper(TestCaseBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<TestCase> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getType()), TestCase::getType, bo.getType());
        lqw.eq(StringUtils.isNotBlank(bo.getCaseNo()), TestCase::getCaseNo, bo.getCaseNo());
        lqw.like(StringUtils.isNotBlank(bo.getName()), TestCase::getName, bo.getName());
        lqw.eq(StringUtils.isNotBlank(bo.getStatus()), TestCase::getStatus, bo.getStatus());
        lqw.eq(StringUtils.isNotBlank(bo.getCurType()), TestCase::getCurType, bo.getCurType());
        lqw.like(StringUtils.isNotBlank(bo.getCurName()), TestCase::getCurName, bo.getCurName());
        lqw.like(StringUtils.isNotBlank(bo.getOrgName()), TestCase::getOrgName, bo.getOrgName());
        return lqw;
    }

    /**
     * 新增案件管理
     */
    @Override
    public Boolean insertByBo(TestCaseBo bo) {
        TestCase add = BeanUtil.toBean(bo, TestCase.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改案件管理
     */
    @Override
    public Boolean updateByBo(TestCaseBo bo) {
        TestCase update = BeanUtil.toBean(bo, TestCase.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(TestCase entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除案件管理
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
